(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('apf.finance.previewController', createPreviewController);
	createPreviewController.$inject = [ '$scope', '$filter', 'Constants', 'formatService', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService','storageService',
			'piDiscountService', 'arithService' ];
	function createPreviewController($scope, $filter, Constants, formatService, modalService, gridService, layoutConfig, validatorConfig, authService, storageService, piDiscountService, arithService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.piDiscountRequest = {};
		ctrl.piDiscountRequest.discountInvoiceList = [];

		ctrl.init = function() {
			$scope.setPageDescription(params.financeBy);
			ctrl.loanGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.tableConfigs.loanTable, null);
			ctrl.loanGridOptions.data = 'vm.piDiscountRequest.assetLoanList';
			ctrl.loanGridOptions.enableRowHeaderSelection = false;
			ctrl.invoiceGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.tableConfigs.invoiceTable, null);
			ctrl.invoiceGridOptions.data = 'vm.piDiscountRequest.discountInvoiceList';
			ctrl.invoiceGridOptions.enableRowHeaderSelection = false;
			
			var piDiscountRequest = params.piDiscountRequest;
			var totalAmt = parseFloat(arithService.add(piDiscountRequest.intTotalAmt, piDiscountRequest.feeTotalAmt));
			if(arithService.eq(piDiscountRequest.loanTotalAmt, 0) || arithService.eq(totalAmt, 0)){
				piDiscountRequest.costCelling = 0;
			}else{
				piDiscountRequest.costCelling = parseFloat(arithService.mul(arithService.div(totalAmt,piDiscountRequest.loanTotalAmt),100)).toFixed(2);
			}
			angular.forEach(piDiscountRequest.discountInvoiceList, function(data) {
				if(data.communityCodeQ != null && data.communityCode != null){
					data.community = data.communityCodeQ + ":" + data.communityCode;
				}
			});
			ctrl.piDiscountRequest = piDiscountRequest;		
		};
		ctrl.viewInvoiceDetails = function(rowEntity){
			var params = {};
			params.invoice = rowEntity;
			modalService.openCustomDialogs({
				'title': 'title.invoice.detail',
				'dialogsModule' : 'apf.invoice.detail',
				'params' : params,
				'btnCallback' : {
					'ok' : function() {
						
					}
				}
			});
		};
		
		ctrl.createPreviewSubmit = function() {
			return authService.createPreviewSubmit(ctrl.piDiscountRequest).then(submitSuccessful, submitFailed);
		};
		
		ctrl.modify = function() {
			$scope.go('apf.finance.modify', ctrl.piDiscountRequest);
		};
		
		ctrl.openDisCountDetail = function(value) {
			modalService.openCustomDialogs({
				'dialogsModule' : 'apf.disccharge.detail',
				'params' : {
					"disCountDetail" : value,
					"loanCcy" : ctrl.piDiscountRequest.loanCcy
				}
			});
		};
		
		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('apf.workflow.wfTxn.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'apf.workflow.wfTxn.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.pidiscount.create',
					'state' : 'apf.finance.create',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			var messages = [];
			if (response.error && response.error[0]) {
				if (response.error[0].endsWith("\n")) {
					messages = (response.error[0].substring(0, response.error[0].length-2)).split("\n");
				} else {
					messages = response.error[0].split("\n");
				}
			}
			modalService.openErrorDialogs({
				'messages' : messages
			});
		}
	}
})(angular);
